﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace PTTK11_HuongRung
{
    public partial class frmDatHang : Form
    {
        public frmDatHang()
        {
            InitializeComponent();

            Timer tmr = new Timer();
            tmr.Tick += new EventHandler(timer_Tick);
            tmr.Start();

            btnDatHang.Visible = false;
        }

        private void frmDatHang_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reLoadNguyenVatLieu();
        }

        private void reLoadNguyenVatLieu()
        {
            NguyenVatLieuBUS bus = new NguyenVatLieuBUS();
            cbxTenNVL.DataSource = bus.GetNguyenVatLieu();
            cbxTenNVL.DisplayMember = "TEN_NGUYEN_VAT_LIEU";
            cbxTenNVL.ValueMember = "NGUYEN_VAT_LIEU_ID";
        }

        private void txtSoLuong_Click(object sender, EventArgs e)
        {
            txtSoLuong.Clear();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int i = dgvDanhSach.Rows.Count + 1;

            for (int j = 0; j < dgvDanhSach.Rows.Count; j++)
                if (cbxTenNVL.Text.CompareTo(dgvDanhSach.Rows[j].Cells[2].Value.ToString()) == 0 && cbxTenNVL.SelectedValue.ToString().CompareTo(dgvDanhSach.Rows[j].Cells[1].Value.ToString()) == 0)
                {
                    MessageBox.Show("Nguyên Vật liệu '" + cbxTenNVL.Text.Trim() + "' đã có trong nguyên liệu.");
                    return;
                }
            dgvDanhSach.Rows.Add(i, cbxTenNVL.SelectedValue, cbxTenNVL.Text, txtSoLuong.Text);
        }

        private void btnGui_Click(object sender, EventArgs e)
        {
            if (txtMaDH.Text.Count() == 0)
            {
                MessageBox.Show("Mã đơn đặt hàng không để trống!");
                txtMaDH.Focus();
                txtMaDH.SelectAll();
                return;
            }

            DatHangBUS dhBUS = new DatHangBUS();
            DatHangDTO dhDTO = new DatHangDTO();
            dhDTO.MADH = txtMaDH.Text;
            dhDTO.NGAYLAP = dtpNgayLap.Value;
            dhDTO.NGAYGIAO = dtpNgayGiao.Value;
            
            dhBUS.ThemDonDatHang(dhDTO);

            int id = dhBUS.GetDonHangID(txtMaDH.Text);
            ChiTietDonDatHangBUS ctdhBUS = new ChiTietDonDatHangBUS();

            for (int i = 0; i < dgvDanhSach.Rows.Count; i++)
            {
                ChiTietDonDatHangDTO ctdhDTO = new ChiTietDonDatHangDTO();
                ctdhDTO.ID = id;
                ctdhDTO.NVLID = int.Parse(dgvDanhSach.Rows[i].Cells[1].Value.ToString());
                ctdhDTO.SOLUONG = int.Parse(dgvDanhSach.Rows[i].Cells[3].Value.ToString());
                ctdhBUS.ThemChiTietDonDatHang(ctdhDTO);
            }
            MessageBox.Show("Gửi đơn hàng thành công.");
            dgvDanhSach.Rows.Clear();
            txtMaDH.Text = "";
            txtSoLuong.Text = "Số lượng";
        }

        private void LoadDSDonDatHang()
        {
            DatHangBUS dhBUS = new DatHangBUS();
            dgvDSDonDatHang.DataSource = dhBUS.GetDSDonDatHang();
        }

        private void btnDanhSachDH_Click(object sender, EventArgs e)
        {
            LoadDSDonDatHang();
            btnDatHang.Visible = true;
            dgvDanhSach.Visible = false;
            dgvDSDonDatHang.Visible = true;

            txtMaDH.Enabled = false;
            dtpNgayLap.Enabled = false;
            dtpNgayGiao.Enabled = false;
            cbxTenNVL.Enabled = false;
            txtSoLuong.Enabled = false;
            btnThem.Enabled = false;
        }

        private void btnDatHang_Click(object sender, EventArgs e)
        {
            btnDatHang.Visible = false;
            dgvDSDonDatHang.Visible = false;
            dgvDanhSach.Visible = true;

            txtMaDH.Enabled = true;
            dtpNgayLap.Enabled = true;
            dtpNgayGiao.Enabled = true;
            cbxTenNVL.Enabled = true;
            txtSoLuong.Enabled = true;
            btnThem.Enabled = true;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ThoiGian.Text = DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmQuanLy frm = new frmQuanLy();
            frm.ShowDialog();
            this.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";

                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }
    }
}
